package com.dap.test.client;

import javax.ws.rs.client.Client;
import javax.ws.rs.client.ClientBuilder;
import javax.ws.rs.client.WebTarget;
import javax.ws.rs.core.Response;

import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;

import com.dap.test.DisconfTestConstant;
import com.dap.test.app.CreateAppTest;
import com.dap.test.app.DeleteAppTest;
import com.dap.test.config.NewConfigFileWithTest;
import com.dap.test.env.CreateEnvTest;
import com.dap.test.env.DeleteEnvTest;

public class DownLoadConfigFile {

	private static String sessionId;
	private static String key = "configItemKeyNewTest"+Math.random();
	private static String value = "configItemValueNewTest"+Math.random();
	private static String appName = "appNameNewTest"+Math.random();
	private static String envName = "envNameNewTest"+Math.random();
	private static String appId ;
	private static String envId;
	private static String configId;
	@Before
	public void before(){
		Client client = ClientBuilder.newClient();
		StringBuffer url = new StringBuffer();
		url.append(DisconfTestConstant.CONSOLE_REST_URI_PREFIX+"/services/sys/login?loginName=admin&password=admin");
		WebTarget target = client.target(url.toString());
		Response response = target.request().get();
		String str = response.getHeaderString("Set-Cookie");
		int m = str.indexOf("=");
		int n = str.indexOf(";");
		sessionId = str.substring(m+1, n);
		//初始化appId、envId
		/*创建App*/
		/*创建Env*/
		CreateAppTest createApp = new CreateAppTest();
		CreateEnvTest createEnv = new CreateEnvTest();
		NewConfigFileWithTest newConfig = new NewConfigFileWithTest();
		String response1 = createApp.createApp(appName,appName,sessionId);
		String response2 = createEnv.createEnv(envName,sessionId);
		appId = response1.substring(response1.indexOf("=")+1,response1.length()-2);
		envId = response2.substring(response2.indexOf("=")+1,response2.length()-2);
		String response3 = newConfig.newConfigItem(appId,"1_0_0_0",envId,key,value,"DAP",sessionId);
		configId = response3.substring(response3.indexOf("=")+1,response3.length()-2);
		
	}
	@After
	public void after(){
		DeleteAppTest deleteApp = new DeleteAppTest();
		DeleteEnvTest deleteEnv = new DeleteEnvTest();
		/*删除Config*/
		StringBuffer url1 = new StringBuffer();//zhengchangzhi
		url1.append(DisconfTestConstant.DISCONF_REST_URI_PREFIX+"/api/web/config/"+configId);
		deleteConfig(url1.toString(),sessionId);
		/*删除Env*/
		deleteEnv.deleteEnvById(envId, sessionId);
		/*删除App*/
		deleteApp.deleteAppById(appId, sessionId);
	}
	@Test
	//app=[无效（不存在）]
	public void test1() {
		StringBuffer url1 = new StringBuffer();//正常情况{"status":1,"message":"","value":"dxddtxtt"} {"status":0,"message":"cannot find this config","value":""}
		url1.append(DisconfTestConstant.DISCONF_REST_URI_PREFIX+"/api/configExtension/file");
		url1.append("?app=999999999999XXXXXXXX");
		url1.append("&version=1_0_0_0");
		url1.append("&env="+envId);
		url1.append("&bankKey=DAP");
		url1.append("&key="+key);
		url1.append("&userId=");
		String response = downLoadConfigItem(url1.toString());
		Assert.assertEquals("返回状态比较", response.contains(""), true);
	}
	
	@Test
	//app=[空]
	public void test2() {
		StringBuffer url1 = new StringBuffer();//正常情况{"status":1,"message":"","value":"dxddtxtt"} {"status":0,"message":"cannot find this config","value":""}
		url1.append(DisconfTestConstant.DISCONF_REST_URI_PREFIX+"/api/configExtension/file");
		url1.append("?app=");
		url1.append("&version=1_0_0_0");
		url1.append("&env="+envId);
		url1.append("&bankKey=DAP");
		url1.append("&key="+key);
		url1.append("&userId=");
		String response = downLoadConfigItem(url1.toString());
		Assert.assertEquals("返回状态比较", response.contains(""), true);
	}
	@Test
	//env=[空]
	public void test3() {
		StringBuffer url1 = new StringBuffer();//正常情况{"status":1,"message":"","value":"dxddtxtt"} {"status":0,"message":"cannot find this config","value":""}
		url1.append(DisconfTestConstant.DISCONF_REST_URI_PREFIX+"/api/configExtension/file");
		url1.append("?app="+appName);
		url1.append("&version=1_0_0_0");
		url1.append("&env=");
		url1.append("&bankKey=DAP");
		url1.append("&key="+key);
		url1.append("&userId=");
		String response = downLoadConfigItem(url1.toString());
		Assert.assertEquals("返回状态比较", response.contains(""), true);
	}
	@Test
	//key=[空]
	public void test4() {
		StringBuffer url1 = new StringBuffer();//正常情况{"status":1,"message":"","value":"dxddtxtt"} {"status":0,"message":"cannot find this config","value":""}
		url1.append(DisconfTestConstant.DISCONF_REST_URI_PREFIX+"/api/configExtension/file");
		url1.append("?app="+appName);
		url1.append("&version=1_0_0_0");
		url1.append("&env="+envName);
		url1.append("&bankKey=DAP");
		url1.append("&key=");
		url1.append("&userId=");
		String response = downLoadConfigItem(url1.toString());
		Assert.assertEquals("返回状态比较", response.contains(""), true);
	}
	@Test
	//bankKey=[空]
	public void test5() {
		StringBuffer url1 = new StringBuffer();//正常情况{"status":1,"message":"","value":"dxddtxtt"} {"status":0,"message":"cannot find this config","value":""}
		url1.append(DisconfTestConstant.DISCONF_REST_URI_PREFIX+"/api/configExtension/file");
		url1.append("?app="+appName);
		url1.append("&version=1_0_0_0");
		url1.append("&env="+envName);
		url1.append("&bankKey=");
		url1.append("&key="+key);
		url1.append("&userId=");
		String response = downLoadConfigItem(url1.toString());
		Assert.assertEquals("返回状态比较", response.contains(""), true);
	}
	@Test
	//version=[空]
	public void test6() {
		StringBuffer url1 = new StringBuffer();//正常情况{"status":1,"message":"","value":"dxddtxtt"} {"status":0,"message":"cannot find this config","value":""}
		url1.append(DisconfTestConstant.DISCONF_REST_URI_PREFIX+"/api/configExtension/file");
		url1.append("?app="+appName);
		url1.append("&version=");
		url1.append("&env="+envName);
		url1.append("&bankKey=DAP");
		url1.append("&key="+key);
		url1.append("&userId=");
		String response = downLoadConfigItem(url1.toString());
		Assert.assertEquals("返回状态比较", response.contains(""), true);
	}
	@Test
	//env=[无效（不存在）]
	public void test7() {
		StringBuffer url1 = new StringBuffer();//正常情况{"status":1,"message":"","value":"dxddtxtt"} {"status":0,"message":"cannot find this config","value":""}
		url1.append(DisconfTestConstant.DISCONF_REST_URI_PREFIX+"/api/configExtension/file");
		url1.append("?app="+appName);
		url1.append("&version=1_0_0_0");
		url1.append("&env=9999999999999GGGGGGG");
		url1.append("&bankKey=DAP");
		url1.append("&key="+key);
		url1.append("&userId=");
		String response = downLoadConfigItem(url1.toString());
		Assert.assertEquals("返回状态比较", response.contains(""), true);
	}
	@Test
	//bankKey=[有效],envId=[有效],version=[有效],appId=[有效],key=[有效],
	public void test8() {
		StringBuffer url1 = new StringBuffer();//
		url1.append(DisconfTestConstant.DISCONF_REST_URI_PREFIX+"/api/configExtension/file");
		url1.append("?app="+appName);
		url1.append("&version=1_0_0_0");
		url1.append("&env="+envName);
		url1.append("&bankKey=DAP");
		url1.append("&key="+key);
		url1.append("&userId=");
		String response = downLoadConfigItem(url1.toString());
		Assert.assertEquals("返回状态比较", response.contains("configItemValueNewTest"), true);
	}
	
	
	public String downLoadConfigItem(String url){
		Client client = ClientBuilder.newClient();
		WebTarget target = client.target(url.toString());
		Response response = target.request().get();
		return response.readEntity(String.class);
	}
	
	//删除配置项
	public String  deleteConfig(String url,String sessionId){
		Client client = ClientBuilder.newClient();
		WebTarget target = client.target(url.toString());
		Response response = target.request().header("EX-JSESSIONID",sessionId).delete();
		return response.readEntity(String.class);
	}


}
